package com.itheima.springboottlias.aspect;


import Pojo.OperateLog;
import com.itheima.springboottlias.Util.JwtUtils;
import com.itheima.springboottlias.commons.CurrentHolder;
import com.itheima.springboottlias.mapper.OperateLogMapper;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/*记录操作日志 的切面类
* */
@Slf4j
@Component
@Aspect
public class OperationLogAspect {
    @Autowired
    private OperateLogMapper operateLogMapper;

    @Autowired
    private HttpServletRequest request;//请求数据
    //切入点表达式：execution(返回值类型 包名.类名.方法名(参数列表))+annotation(注解类型)
    //通知类型:环绕通知
    @Around("execution(* com.itheima.springboottlias.service.*Service.*(..)) && @annotation(com.itheima.springboottlias.anno.LogOperation)")
    //使用@annotation注解时需要注意要将注解加在实现类上
    public Object recordOperationLog(ProceedingJoinPoint pjp) throws Throwable {
        //记录操作日志:收集操作日志信息,添加到操作日志表中
        long start = System.currentTimeMillis();
        Object  result = pjp.proceed();
        long end = System.currentTimeMillis();
        long costTime = end - start;


        //1.定义操作日志对象,收集操作日志信息
        OperateLog log = new OperateLog();
        log.setOperateTime(LocalDateTime.now());//操作时间
        log.setClassName(pjp.getTarget().getClass().getName());//操作类名
        log.setMethodName(pjp.getSignature().getName());//操作方法名
        log.setMethodParams(pjp.getArgs().toString());//操作参数
        log.setReturnValue(result+"");//操作返回值
        log.setCostTime(costTime);//操作耗时
        log.setOperateEmpId(CurrentHolder.getCurrentId());//操作员工ID

        //2.调用操作日志的持久层接口,添加操作日志到操作日志表中
        operateLogMapper.insert(log);
        return result;
    }
}
